package main

import (
	"fmt"
	"gofirst/ch05string/fmtprint"
	"strings"
)

func testStringLen() {
	fmt.Println("in func testStringLen: ")

	var name string = "慕课网"

	//1.字符串长度，len返回的是字节的长度，涉及到中文就产生了变化
	//unicode字符集，存储的时候需要编码utf-8规则，utf8编码是一个动态的编码规则
	//utf8编码，中文是3个字节，还能够用一个字节表示英文
	fmt.Println("len(name): ", len(name))
	// 类型转换
	nameArr := []rune(name)
	fmt.Println("len(name),by rune arr: ", len(nameArr))
}

func testEscapeString() {
	fmt.Println("in func testEscapeString: ")

	//转义字符,一旦出现\,则解释器会认为其后的一个字符和他是一体的
	name := "\"慕课网\""
	fmt.Println("转义字符(\\\"): ", name)

	//元字符串（``）
	fmt.Println("使用（`）引起的字符串，就不用转义了：", `"慕课网", c:\hello\world`)
}

func testSubString() {
	fmt.Println("in func testSubString: ")

	//子串
	var name = "hello, \"慕课网\""

	//1.是否包含某个子串
	fmt.Println("strings.Contains: ", strings.Contains(name, "慕课网"))
	fmt.Println("strings.index: ", strings.Index(name, "慕课网"))

	//2.统计字符出现的次数
	fmt.Println("strings.Count", strings.Count(name, "l"))

	//3.前缀和后缀
	fmt.Println("strings.HasPrefix(h) for", name, " result: ", strings.HasPrefix(name, "h"))
	fmt.Println("strings.HasPrefix(b) for", name, " result: ", strings.HasPrefix(name, "b"))
	fmt.Println("strings.HasSuffix(\") for", name, " result: ", strings.HasSuffix(name, `"`))
	fmt.Println("strings.HasSuffix(网) for", name, " result: ", strings.HasSuffix(name, "网"))

	//4.大小写转换
	str := "HeLlo"
	fmt.Println("strings.ToLower for", str, " result: ", strings.ToLower(str))
	fmt.Println("strings.ToUpper for", str, " result: ", strings.ToUpper(str))
	fmt.Println("strings.ToTitle for", str, " result: ", strings.ToTitle(str))

	//5.字符串比较, 就是ascii码比较
	str1 := "a"
	str2 := "b"
	//pos1 < pos2 -> -1, else 1, 否则 0
	fmt.Println("strings.Compare for", str1, " and ", str2, " result: ", strings.Compare(str1, str2))
	str1 = "ab"
	fmt.Println("strings.Compare for", str1, " and ", str2, " result: ", strings.Compare(str1, str2))
	str1 = "b"
	fmt.Println("strings.Compare for", str1, " and ", str2, " result: ", strings.Compare(str1, str2))

	//6.去掉首尾的空格
	str = "          hello   world         "
	fmt.Println("strings.TrimSpace for", str, " result: ", strings.TrimSpace(str))
	str = "hhellooooo"
	fmt.Println("strings.TrimLeft for", str, " result: ", strings.TrimLeft(str, "h"))
	fmt.Println("strings.TrimRight for", str, " result: ", strings.TrimRight(str, "o"))
	str = "oohellooooo"
	fmt.Println("strings.Trim for", str, " result: ", strings.Trim(str, "o"))

	//7.split
	fmt.Println("strings.Split for", str, " result: ", strings.Split(str, "e"))

	//8.join: 将字符串数组连接起来
	strArr := []string{"He", "ooo", "world"}
	fmt.Println("strings.Join for", strArr, " result: ", strings.Join(strArr, "-"))

	//9.replace
	str = "age: 18, 18, 18"
	fmt.Println("strings.Replace for", str, " result: ", strings.Replace(str, "18", "19", 1))
	fmt.Println("strings.Replace for", str, " result: ", strings.Replace(str, "18", "19", -1))

}

func main() {
	//	//字符串长度
	//	testStringLen()
	//	//转义字符
	//	testEscapeString()
	//	//子串
	//	testSubString()
	//

	//格式化输出
	fmtprint.TestFmtPrint()
}
